package ch.hearc.infrarouge;

import static ch.hearc.infrarouge.tools.ConnectionTools.setupProxy;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import ch.hearc.infrarouge.database.DBCache;
import ch.hearc.infrarouge.models.Forum;
import ch.hearc.infrarouge.models.ForumThread;
import ch.hearc.infrarouge.models.Reply;
import ch.hearc.infrarouge.models.ThreadMessage;
import ch.hearc.infrarouge.models.User;
import ch.hearc.infrarouge.tools.DatabaseTools;
import ch.hearc.infrarouge.tools.DebugTools;
import ch.hearc.infrarouge.tools.ParserTools;

public class Main {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		// setup the proxy settings
		setupProxy();

		// ParserTools.parse(Preferences.infrarougeForumMinderURL);
		// ParserTools.parse(Preferences.infrarougeForumAout2012URL);

		String[] forumURLs = {
				"http://www.infrarouge.ch/ir/1974-special-votation-initiative-minder-solution-salaires-abusifs",
				"http://www.infrarouge.ch/ir/2001-special-votation-peuple-pour-elire-conseil-federal",
				"http://www.infrarouge.ch/ir/2002-votation-asile-encore-durcissement",
				"http://www.infrarouge.ch/ir/1990-libre-circulation-faut-tirer-frein-main",
				"http://www.infrarouge.ch/ir/1992-fraude-fiscale-guerre-totale",
				"http://www.infrarouge.ch/ir/1983-nouveau-pape-pour-quels-changements",
				"http://www.infrarouge.ch/ir/1966-tuerie-daillon-trop-rsquo-armes-suisse", };

		for (String url : forumURLs) {
			ParserTools.parse(url);
		}

		// show list of replies
		// showReplies();

		// show DB cache info
		System.out.println(DBCache.getInstance());

//		showUsers();

		 fillDatabase();
	}

	private static void showUsers() {
		List<User> users = DBCache.getInstance().getUsers();

		// sort by user ID ASC
		Collections.sort(users, new Comparator<User>() {
			public int compare(User o1, User o2) {
				return (int) (o1.getId() - o2.getId());
			};
		});

		for (User user : users) {
			System.out.println(user);
		}
	}

	private static void fillDatabase() throws ClassNotFoundException {

		List<User> users = DBCache.getInstance().getUsers();
		List<Forum> forums = DBCache.getInstance().getForums();
		List<ForumThread> forumThreads = DBCache.getInstance()
				.getForumThreads();
		List<ThreadMessage> threadMessages = DBCache.getInstance()
				.getThreadMessages();
		List<Reply> replies = DBCache.getInstance().getReplies();

		System.out.println("Inserting users into database...");
		DatabaseTools.insertUsers(users);
		System.out.println("Inserting forums into database...");
		DatabaseTools.insertForums(forums);
		System.out.println("Inserting forum threads into database...");
		DatabaseTools.insertForumThreads(forumThreads);
		System.out.println("Inserting thread messages into database...");
		DatabaseTools.insertThreadMessages(threadMessages);
		System.out.println("Inserting replies into database...");
		DatabaseTools.insertReplies(replies);
		System.out.println("... done!");
	}

	private static void showReplies() {
		List<Reply> replies = DBCache.getInstance().getReplies();

		for (Reply reply : replies) {
			User from = reply.getReplyFrom();
			User to = reply.getReplyTo();

			String thread = reply.getThreadMessage().getForumThread()
					.getTitle();

			DebugTools.debugln(from + " -> " + to + " in " + thread);
		}
	}
}
